package com.wtgroup.demo.common.util;

import com.wtgroup.demo.common.bean.MultiTreeNode;
import leetcodeauto.common.ListNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author nisus
 * @version 0.1
 * @since 2023/5/6 10:50
 */
public class Mock {

    public static void main(String[] args) {
        TreeNode head = multiTree(new Integer[]{720, -126, -279, 592});
        System.out.println(head);
    }


    // [mock 多叉树]
    // values 每层从左到右的值
    public static MultiTreeNode multiTree(int[] values, int forkCnt) {
        if (values == null || values.length == 0) {
            return null;
        }
        int vali = 0;
        MultiTreeNode head = new MultiTreeNode().setValue(values[vali++]);
        List<MultiTreeNode> lastLevel = new ArrayList<>();
        lastLevel.add(head);
        for (;vali < values.length;) {
            List<MultiTreeNode> lastLevel_new = new ArrayList<>();
            for (MultiTreeNode n : lastLevel) {
                // 随机 nexts
                for (int i = 0; i < forkCnt; i++) {
                    if (vali == values.length) {
                        break;
                    }

                    MultiTreeNode e = new MultiTreeNode().setValue(values[vali++]);
                    n.addNext(e);
                    lastLevel_new.add(e);
                }
            }
            lastLevel = lastLevel_new;
        }

        return head;
    }

    // [mock 多叉树]
    public static MultiTreeNode multiTree(int maxLevel, int maxNextCnt) {
        MultiTreeNode head = new MultiTreeNode().setValue(RandomUtil.randomInt(-1000, 1001));
        List<MultiTreeNode> lastLevel = new ArrayList<>();
        lastLevel.add(head);
        for (int i = 1; i < maxLevel; i++) {
            List<MultiTreeNode> lastLevel_new = new ArrayList<>();
            for (MultiTreeNode n : lastLevel) {
                // 随机 nexts
                int[] ints = RandomUtil.randomArray(RandomUtil.randomInt(maxNextCnt+1), -1000, 1001);
                for (int anInt : ints) {
                    MultiTreeNode e = new MultiTreeNode().setValue(anInt);
                    n.addNext(e);
                    lastLevel_new.add(e);
                }
            }
            lastLevel = lastLevel_new;
        }

        return head;
    }

    // LeetCode 专用 ListNode
    public static ListNode listNode(int[] values) {
        ListNode dummy = new ListNode();
        ListNode tail = dummy;
        for (int num : values) {
            tail = tail.next = new ListNode(num);
        }
        return dummy.next;
    }

    // [mock 二叉树]
    // values 每层从左到右的值
    public static TreeNode multiTree(Integer[] values) {
        if (values == null || values.length == 0) {
            return null;
        }
        int vali = 0;
        TreeNode head = new TreeNode(values[vali++]);
        List<TreeNode> lastLevel = new ArrayList<>();
        lastLevel.add(head);
        for (;vali < values.length;) {
            List<TreeNode> lastLevel_new = new ArrayList<>();
            for (TreeNode n : lastLevel) {
                // 随机 nexts
                for (int i = 0; i < 2; i++) {
                    if (vali == values.length) {
                        break;
                    }

                    Integer value = values[vali++];
                    if (value != null) {
                        TreeNode e = new TreeNode(value);
                        if (i == 0) {
                            n.left = e;
                        } else {
                            n.right = e;
                        }
                        lastLevel_new.add(e);
                    }
                }
            }
            lastLevel = lastLevel_new;
        }

        return head;
    }

}
